define(function () {

    /*
    config选项
    dataList
    userType
    editable 默认false
    hasAvatar 默认false

    实例方法
    getCount 返回数量
    getIdList 返回id列表
    getDataList 返回数据列表
    update 更新列表

    */
    var ContactViewList = function (config) {
        var viewlist = this
        var defaultAvatarImgSrc = 'img/avatar.png'

        config.editable || (config.editable = false)
        config.hasAvatar || (config.hasAvatar = false)

        function Contact (contactData,hasAvatar) {
            var contact = this
            contact.data = {}
            contact.$dom = $('<li class="contact" data-contactid="'+ contactData.id +'"></li>')
            if(config.hasAvatar) {
            	contact.$dom.addClass('hasAvatar');
                contact.$avatarBox = $('<div class="icon icon-size-40 icon-avatar"></div>').appendTo(contact.$dom)
                contact.$avatarImg = $('<img>').appendTo(contact.$avatarBox)
            }

            contact.$name = $('<div class="name"></div>').appendTo(contact.$dom)
            contact.$pinyin = $('<div class="pinyin"></div>').appendTo(contact.$dom)
            contact.$description = $('<div class="description"></div>').appendTo(contact.$dom)

            if(config.editable) {
                contact.$removeBtn = $('<div class="icon icon-size-16 icon-remove"></div>').appendTo(contact.$dom)
                contact.$removeBtn.on('click', function () {
                    viewlist.contactList.remove(contactData.id)
                    contact.$dom.removeClass('checked')
                })
            }

            contact.show(contactData)
        }

        Contact.prototype = {
            show: function (contactData) {
                var contact = this
                if(config.hasAvatar) {
                    contact.$avatarImg.attr('src', contactData.avatar || defaultAvatarImgSrc)
                }

                contact.$name.text(contactData.name)
                contact.$pinyin.text(contactData.name)
                contact.$description.text(contactData.phone)
            }
        }

        function ContactList(dataList) {
            this.$dom = $('<ul class="contactViewList"></ul>')
            this.data = {}
            this.newList = {}

            if(dataList) {
                this.init(dataList)
            }
        }

        ContactList.prototype = {
            getCount: function () {
                return Object.keys(this.newList).length
            },
            getIdList: function () {
                return Object.keys(this.newList)
            },
            getDataList: function () {
                return this.data
            },
            init: function (dataList) {
                var contactList = this
                for (var key in dataList) {
                    if (dataList.hasOwnProperty(key)) {
                        var contactData = dataList[key]
                        if(contactData.avatar == '') {
                            contactData.avatar = defaultAvatarImgSrc
                        }

                        this.data[contactData.id] = contactData
                        this.newList[contactData.id] = new Contact(contactData)
                        this.$dom.append(this.newList[contactData.id].$dom)
                    }
                }
            },
            update: function (dataList) {
                this.newList = {}
                this.data = {}
                this.$dom.html('')
                this.init(dataList)
            },
            remove: function (id) {
                if(Object.keys(this.newList).length) {
                    this.newList[id].$dom.remove()
                    delete this.newList[id]
                    delete this.data[id]
                }
            }
        }

        viewlist.contactList = new ContactList(config.dataList)
        viewlist.$dom = viewlist.contactList.$dom
        viewlist.getCount = function () {
            return this.contactList.getCount()
        }
        viewlist.getIdList = function () {
            return this.contactList.getIdList()
        }
        viewlist.update = function (dataList) {
            this.contactList.update(dataList)
        }
        viewlist.getDataList = function () {
            return this.contactList.getDataList()
        }
    }

    window.ContactViewList = ContactViewList
    return ContactViewList
})
